package com.lizhangjun.code;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Scanner;


public class CodeGenerator {

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        //代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        //替换路径
        //String projectPath = System.getProperty("user.dir");
        String projectPath = "D:\\TechSPA\\QniQni_API\\src\\main\\java\\com\\lizhangjun\\code";
        gc.setOutputDir(projectPath);    //生成文件的输出目录,默认值：D 盘根目录
        gc.setFileOverride(true);        //是否覆盖已有文件,默认值：false
        gc.setAuthor("lizhangjun");      //设置开发作者
        gc.setOpen(false);               //是否打开输出目录，默认值：true
        gc.setDateType(DateType.ONLY_DATE); //时间类型对应策略,只使用 java.util.date 代替
        gc.setIdType(IdType.AUTO); //指定生成的主键的ID类型
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://192.168.1.76:3306/qniqni?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.lizhangjun");
        //TODO
        //pc.setModuleName(scanner("模块名"));
        pc.setModuleName("");
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        //TODO
        //strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
//        String tables = "sys_log,sys_macro,sys_resource,sys_role,sys_role_resource,sys_user_role," +
//                "xa_ad,xa_ad_position,xa_collect,xa_comment,xa_config,xa_evaluation,xa_feedback," +
//                "xa_reply,xa_user_admin,xa_user_base,xa_user_enterprise,xa_user_guard";
        String tables = "q_channel,q_item,q_user";
        strategy.setInclude(tables.split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.execute();
    }

}
